package com.qupai.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qupai.constant.StatusConstant;
import com.qupai.entity.Activities;
import com.qupai.result.Result;
import com.qupai.service.ActivitiesService;
import com.qupai.vo.ActivitiesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("userActivitiesController")
@RequestMapping("/user/activities")
@Slf4j
@Tag(name = "志愿-C端-活动浏览接口")
public class ActivitiesController {
    @Resource
    private ActivitiesService activitiesService;

    @GetMapping("/list")
    @Operation(summary = "根据学院分类id查询活动")
    public Result<List<ActivitiesVO>>list(Long collegesId){
        Activities activities=new Activities();
        activities.setCollegesId(collegesId);
        activities.setStatus(StatusConstant.ENABLE);

        List<ActivitiesVO> activitiesVOList = activitiesService.list(activities);
        return Result.success(activitiesVOList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询活动")
    public Result<ActivitiesVO>getById (@PathVariable Long id){
        log.info("根据id查询活动:{}",id);
        ActivitiesVO activitiesVO=activitiesService.getById(id);
        return Result.success(activitiesVO);
    }

    @Operation(summary = "根据活动名称模糊查询活动")
    @GetMapping("/searchByName")
    public Result<List<ActivitiesVO>> searchByName(String name){
        QueryWrapper<Activities>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().like(Activities::getName,name);
        List<Activities> list = activitiesService.list(queryWrapper);
        List<ActivitiesVO> activitiesVOList=new ArrayList<>();
        for (Activities activities : list) {
            ActivitiesVO activitiesVO=new ActivitiesVO();
            BeanUtils.copyProperties(activities,activitiesVO);
            activitiesVOList.add(activitiesVO);
        }
        return Result.success(activitiesVOList);
    }


}
